//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.dao
{
	import __AS3__.vec.Vector;
	
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	
	import reader.model.vo.UserVO;
	
	public class AccountDAO extends BaseDAO implements IAccountDAO
	{
		//////////////////////////////////////////////////////////////////////////////
		//SQLStatement queries
		
		private static const INSERT_USER:String = "INSERT INTO user (culture, userName, email, password, creatDate) VALUES(:culture, :userName, :email, :password, :creatDate)";
		private static const UPDATE_USER:String = "UPDATE user SET culture=:culture, displayIndex=:displayIndex, email=:email, isGuide=:isGuide, isMarkAll=:isMarkAll, isReadNew=:isReadNew, isRollRead=:isRollRead, isSetRead=:isSetRead, isLabelNew:isLabelNew WHERE userId=:userId";
		private static const UPDATE_GET_BLOG_TYPE:String = "UPDATE user SET isReadNew=:isReadNew WHERE userId=:userId";
		private static const UPDATE_SHOW_LABEL_TYPE:String = "UPDATE user SET isLabelNew=:isLabelNew WHERE userId=:userId";
		private static const UPDATE_AUTOSAVE_TYPE:String = "UPDATE user SET isAutoSave=:isAutoSave WHERE userId=:userId";
		private static const UPDATE_PASSWORD:String = "UPDATE user SET password=:password WHERE userId=:userId";
		private static const DELETE_USER:String = "DELETE FROM user WHERE userId=:userId";
		private static const SELETE_USER:String = "SELECT * FROM user WHERE userId=:userId";
		private static const SELETE_USER_BY_NAME:String = "SELECT * FROM user WHERE userName=:userName";
		private static const SELECT_COUNT:String = "SELECT COUNT(userId) FROM user WHERE userName=:userName";
		
		//////////////////////////////////////////////////////////////////////////////
		//private properties
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function AccountDAO(connection:SQLConnection)
		{
			super(connection);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		//----------------------------------------------------------------------------
		//
		// INSERT
		//
		//----------------------------------------------------------------------------
		
		public function insertUser(user:UserVO):int
		{
			var sqlResult:SQLResult = execute(INSERT_USER, {culture: user.culture, userName: user.userName, email: user.email, password: user.password, creatDate: new Date()});
            return int(sqlResult.lastInsertRowID);
		}
		
		//----------------------------------------------------------------------------
		//
		// UPDATE
		//
		//----------------------------------------------------------------------------
		
		public function updateUser(user:UserVO):Boolean
		{
			var obj:Object = {};
			obj.culture = user.culture;
			obj.displayIndex = user.displayIndex;
			obj.email =  user.email;
			obj.isGuide = user.isGuide;
			obj.isMarkAll = user.isMarkAll;
			obj.isReadNew = user.isReadNew;
			obj.isRollRead = user.isRollRead;
			obj.isSetRead = user.isSetRead;
			obj.isLabelNew = user.isLabelNew;
			obj.userId = user.userId;
            return update(UPDATE_USER, obj);
		}
		
		public function updateGetBlogType(user:UserVO):Boolean
		{
			return update(UPDATE_GET_BLOG_TYPE, {isReadNew: user.isReadNew, userId:user.userId});
		}
		
		public function updateShowLabelType(user:UserVO):Boolean
		{
			return update(UPDATE_SHOW_LABEL_TYPE, {isLabelNew: user.isLabelNew, userId:user.userId});
		}
		
		public function updateAutoSaveType(user:UserVO):Boolean
		{
			return update(UPDATE_AUTOSAVE_TYPE, {isAutoSave: user.isAutoSave, userId:user.userId});
		}
		
		public function updatePassword(user:UserVO):Boolean
		{
			return update(UPDATE_PASSWORD, {password: user.password, userId:user.userId});
		}
		
		//----------------------------------------------------------------------------
		//
		// DELETE
		//
		//----------------------------------------------------------------------------
		
		public function deleteUser(user:UserVO):Boolean
		{
            return drop(DELETE_USER, {userId:user.userId});
		}
		
		//----------------------------------------------------------------------------
		//
		// SELETE
		//
		//----------------------------------------------------------------------------
		
		public function selectUser(userid:int):UserVO
		{
			return select(SELETE_USER, {userId: userid});
		}
		
		public function selectUserByName(username:String):UserVO
		{
			return select(SELETE_USER_BY_NAME, {userName: username});
		}
		
		private function select(query:String, data:Object):UserVO
		{
			var result:SQLResult = execute(query, data);
			if(!result || !result.data)
			{
				return null;
			}
			
			var dataArray:Vector.<UserVO> = new Vector.<UserVO>();
						
			for each(var obj:Object in result.data)
			{
				var _data:UserVO = new UserVO();
				_data.userName = obj["userName"];
				_data.password = obj["password"];
				_data.userId = obj["userId"];
				_data.culture = obj["culture"];
				_data.displayIndex = obj["displayIndex"];
				_data.email = obj["email"];
				_data.isGuide = obj["isGuide"];
				_data.isMarkAll = obj["isMarkAll"];
				_data.isReadNew = obj["isReadNew"];
				_data.isRollRead = obj["isRollRead"];
				_data.isSetRead = obj["isSetRead"];
				_data.isLabelNew = obj["isLabelNew"];
				_data.isAutoSave = obj["isAutoSave"];
				
				dataArray.push(_data);
			}
            return dataArray[0];
		}
		
		//----------------------------------------------------------------------------
		//
		// COUNT
		//
		//----------------------------------------------------------------------------
		
		public function getCount(username:String):uint
		{
			return count(SELECT_COUNT, {userName: username});
		}
	}
}